﻿using System;
using System.Collections.Generic;
using System.Text;

namespace curbside.Web.UI.WebControls
{
    /// <summary>
    /// 日历控件
    /// </summary>
    public class Calendar : TextBox
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            base.MaxLength = 10;
            base.CssClass = "Wdate";
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            string jsEventCode = @"$('#" + this.ClientID + "').click(function(){WdatePicker(" + FormatString + ")});";
            helper.AddStartupScript(this.ClientID, jsEventCode, this.Page);
        }

        /// <summary>
        /// 设置或获取日期
        /// </summary>
        public override string Text
        {
            get { return base.Text; }
            set
            {
                DateTime t;
                if (DateTime.TryParse(value, out t))
                    base.Text = t.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 设置或获取自定义日期控件格式
        /// </summary>
        public bool UseCustomDateFormat
        {
            get
            {
                object obj = this.ViewState["UseCustomDateFormat"];
                if (obj == null) return false;
                return (bool)obj;
            }
            set { this.ViewState["UseCustomDateFormat"] = value; }
        }

        /// <summary>
        /// 设置或获取是否支持周显示
        /// </summary>
        public bool IsShowWeek
        {
            get
            {
                object obj = this.ViewState["IsShowWeek"];
                if (obj == null) return false;
                return (bool)obj;
            }
            set { this.ViewState["IsShowWeek"] = value; }
        }

        /// <summary>
        /// 设置或获取星期的第一天
        /// </summary>
        public DayOfWeek FirstDayOfWeek
        {
            get
            {
                object obj = this.ViewState["FirstDayOfWeek"];
                if (obj == null) return DayOfWeek.Sunday;
                return (DayOfWeek)obj;
            }
            set { this.ViewState["FirstDayOfWeek"] = value; }
        }

        /// <summary>
        /// 设置或获取最小日期
        /// </summary>
        public DateTime MinDate
        {
            get
            {
                object obj = this.ViewState["MinDate"];
                if (obj == null) return DateTime.MinValue;
                return (DateTime)obj;
            }
            set { this.ViewState["MinDate"] = value; }
        }

        /// <summary>
        /// 设置或获取最大日期
        /// </summary>
        public DateTime MaxDate
        {
            get
            {
                object obj = this.ViewState["MaxDate"];
                if (obj == null) return DateTime.MaxValue;
                return (DateTime)obj;
            }
            set { this.ViewState["MaxDate"] = value; }
        }

        /// <summary>
        /// 设置或获取语言
        /// </summary>
        public CalendarLanguage Language
        {
            get
            {
                object obj = this.ViewState["Language"];
                if (obj == null) return CalendarLanguage.Chinese;
                return (CalendarLanguage)obj;
            }
            set { this.ViewState["Language"] = value; }
        }

        /// <summary>
        /// 设置或获取皮肤
        /// </summary>
        public CalendarSkin Skin
        {
            get
            {
                object obj = this.ViewState["Skin"];
                if (obj == null) return CalendarSkin.Deault;
                return (CalendarSkin)obj;
            }
            set { this.ViewState["Skin"] = value; }
        }

        /// <summary>
        /// 设置或获取起始日期
        /// </summary>
        public DateTime StartDate
        {
            get
            {
                object obj = this.ViewState["StartDate"];
                if (obj == null) return DateTime.MinValue;
                return (DateTime)obj;
            }
            set { this.ViewState["StartDate"] = value; }
        }

        /// <summary>
        /// 设置或获取当日期框无论是何值，都始终启用起始日期
        /// </summary>
        public bool AlwaysUseStartDate
        {
            get
            {
                object obj = this.ViewState["AlwaysUseStartDate"];
                if (obj == null) return false;
                return (bool)obj;
            }
            set { this.ViewState["AlwaysUseStartDate"] = value; }
        }

        /// <summary>
        /// 设置或获取是否进行双月显示
        /// </summary>
        public bool DoubleCalendar
        {
            get
            {
                object obj = this.ViewState["DoubleCalendar"];
                if (obj == null) return false;
                return (bool)obj;
            }
            set { this.ViewState["DoubleCalendar"] = value; }
        }



        public string FormatString
        {
            get
            {
                if (UseCustomDateFormat)
                {
                    string formatStr = this.ViewState["FormatString"] as string;
                    if (formatStr != null) return formatStr;
                    return string.Empty;
                }

                StringBuilder sb = new StringBuilder();
                sb.Append("{");

                if (IsShowWeek)
                {
                    sb.Append("isShowWeek:true,");
                }

                if (FirstDayOfWeek != DayOfWeek.Sunday)
                {
                    sb.Append("firstDayOfWeek:" + (int)FirstDayOfWeek + ",");
                }

                if (MinDate != DateTime.MinValue)
                {
                    sb.Append("minDate:'" + MinDate.ToString("yyyy-MM-dd") + "',");
                }

                if (MaxDate != DateTime.MaxValue)
                {
                    sb.Append("maxDate:'" + MaxDate.ToString("yyyy-MM-dd") + "',");
                }

                if (StartDate != DateTime.MinValue)
                {
                    sb.Append("startDate:'" + StartDate.ToString("yyyy-MM-dd") + "',");
                }

                if (AlwaysUseStartDate)
                {
                    sb.Append("alwaysUseStartDate:true,");
                }

                if (DoubleCalendar)
                {
                    sb.Append("doubleCalendar:true,");
                }


                if (Language != CalendarLanguage.Chinese)
                {
                    if (Language == CalendarLanguage.English)
                        sb.Append("lang:'en',");
                }

                if (Skin != CalendarSkin.Deault)
                {
                    if (Skin == CalendarSkin.whyGreen)
                    {
                        sb.Append("skin:'whyGreen',");
                    }
                    else if (Skin == CalendarSkin.YcloudRed)
                    {
                        sb.Append("skin:'YcloudRed',");
                    }
                    else if (Skin == CalendarSkin.Blue)
                    {
                        sb.Append("skin:'blue',");
                    }
                }



                sb.Append("}");

                string str = sb.ToString();
                if (str == "{}")
                {
                    return string.Empty;
                }
                else
                {
                    str = str.Replace(",}", "}");
                }

                return str;
            }

            set { this.ViewState["FormatString"] = value; }
        }
    }

    /// <summary>
    /// 日历控件语言枚举
    /// </summary>
    public enum CalendarLanguage
    {
        Chinese,
        English
    }

    /// <summary>
    /// 日历控件皮肤枚举
    /// </summary>
    public enum CalendarSkin
    {
        Deault,
        whyGreen,
        YcloudRed,
        Blue,
    }
}
